//
//  UserListViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserListViewController.h"
#import "UserLoginViewController.h"
#import "MyMovieViewController.h"

@interface UserListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIButton *btn;

@end

@implementation UserListViewController

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    NSLog(@"----%@",isLogin);
    if ([isLogin isEqualToString:@"1"]) {
        [self.btn setTitle:@"注销" forState:UIControlStateNormal];
    }else{
        [self.btn setTitle:@"登陆" forState:UIControlStateNormal];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.userTableView.delegate = self;
    self.userTableView.dataSource = self;
    [self.view addSubview:self.userTableView];
    [self.userTableView release];
    
    [self.userTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"userCell"];
    
    self.navigationItem.title = @"我的";
    
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(10, 0, 40, 40);
    [self.btn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btn];
    
}

- (void)loginAction:(UIButton *)btn{

    if ([self.btn.titleLabel.text isEqualToString:@"注销"]) {
//        [self.btn setTitle:@"登陆" forState:UIControlStateNormal];
        
        //提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要注销么" preferredStyle:1];
        [self presentViewController:alert animated:1 completion:^{
        
        
        }];
        
        UIAlertAction *cancelAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        
        UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
            [self.btn setTitle:@"登陆" forState:UIControlStateNormal];
        }];
        
        [alert addAction:cancelAct];
        [alert addAction:sureAct];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isLogin"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"name"];
    }else{
    
    UserLoginViewController *loginVC = [[UserLoginViewController alloc] init];
    UINavigationController *loginNavi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:loginNavi animated:1 completion:^{
        
        
    }];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellId = @"userCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }else if (indexPath.row == 1){
    
        cell.textLabel.text = @"我的电影";
    }else if (indexPath.row == 2){
    
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 1) {
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"0"]) {
            
            UserLoginViewController *loginVC = [[UserLoginViewController alloc] init];
            UINavigationController *loginNavi = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:loginNavi animated:1 completion:^{
                
                
            }];

            
        }else{
        
        MyMovieViewController *myMovieVC = [[MyMovieViewController alloc] init];
        [self.navigationController pushViewController:myMovieVC animated:1];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
